﻿/// <reference path="/Scripts/Helpers/Functions.js" />
/// <reference path="/Scripts/jquery-1.7.2.js" />
/// <reference path="/Scripts/jquery.mobile-1.1.0.js" />

// namespace Enregistrer [Reception]
var Enregistrer = (function()
{
    var my = {},
    _gtin,
    _emplacement,
    _currentStep = 0;

    my.loaded = function()
    {
        initEvents();
    };

    // INITIALISE LES EVENEMENTS DE LA PAGE
    function initEvents()
    {
        // ÉVÈNEMENT : APPUI SUR LE BOUTON RETOUR
        $("#btn_retour").click(function()
        {
            switch (_currentStep)
            {
                case 0:
                    {
                        window.location = "/Accueil";
                        break;
                    }
                case 1:
                    {
                        $("#title").text("Scannez une marchandise");
                        $("#btn_valid").attr("href", "#marchandises");
                        $("#icone_marchandise").css("display", "inline");
                        $("#icone_emplacement").css("display", "none");
                        break;
                    }
            }
            _currentStep--;

        });

        $("#btn_valid").click(function()
        {
            switch (_currentStep)
            {
                case 0:
                    {
                        $("#content").slideUp(500);
                        $(".popup").slideDown(500);
                        $("#marchandises").show();
                        break;
                    }
                case 1:
                    {
                        $("#content").slideUp(500);
                        $(".popup").slideDown(500);
                        $("#emplacements").show();
                        break;
                    }
            }
        });

        $(".marchandise").click(function()
        {
            _gtin = $(this).attr("data-gtin");
            $("#marchandises").hide();
            $("#content").slideDown(500);
            $(".popup").slideUp(500);
            dispatch();
        });

        $(".emplacement").click(function()
        {
            _emplacement = $(this).attr("data-codeemplacement");
            $('#emplacements').hide();
            $("#content").slideDown(500);
            $(".popup").slideUp(500);
            dispatch();
        });

        $("#btn_ok").click(function()
        {
            window.location = "/Reception/Enregistrer";
        });
    }

    function dispatch()
    {
        switch (_currentStep)
        {
            case 0:
                {
                    $("#title").text("Scannez un emplacement");
                    $("#btn_valid").attr("href", "#emplacements");
                    $("#icone_emplacement").css("display", "inline");
                    $("#icone_marchandise").css("display", "none");
                    break;
                }
            case 1:
                {
                    record();
                    break;
                }
        }
        _currentStep++;
    }

    function record()
    {
        $.ajax(
        {
            url: "/Reception/CreerMouvement",
            type: "POST",
            data: JSON.stringify({ gtin: _gtin, emplacement: _emplacement }),
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            success: function(data, textStatus, jqXHR)
            {
                if (data[0] == "-1" || data[0] == "0")
                {
                    // Envoi d'un message d'erreur
                    alert(data[1]);
                    window.location.reload();
                    return;
                }
                else
                {
                    // Mouvement enregistré

                    $("#title").text("Marchandise enregistrée");
                    $("#icone_ok").css("display", "inline");
                    $("#icone_emplacement").css("display", "none");
                    $("#btn_ok").css("display", "inline");
                    $("#btn_scanner").css("display", "none");
                    $("#btn_retour").css("visibility", "collapse");
                    $("#btn_valid").attr("href", "#");
                }
            },
            error: function(jqXHR, textStatus, errorThrown)
            {
                alert(errorThrown);
                window.location.reload();
            }
        });
    }

    return my;
} ());

Functions.addLoadListener(Enregistrer.loaded);

$(document).bind("mobileinit", function()
{
    //apply overrides here
    $.mobile.defaultPageTransition = "flow";
});